﻿/***
    Copyright 2012 Stijn Van Antwerpen
  
    This file is part of Qasparov.

    Qasparov is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Qasparov is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Qasparov. If not, see <http://www.gnu.org/licenses/>.

 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using C = Com.Oxilly.Qasparov.QsbClient.QsbCommandService;
using Com.Oxilly.Qasparov.QsbClient;

namespace Com.Oxilly.Qasparov.CommandConsole
{
    class Program
    {
        API api;

        static void Main(string[] args)
        {
            (new Program()).Run();
        }

        private void Run()
        {
            try
            {
                api = ClientFactory.CreateClientApi();
              
                Console.WriteLine("Ready to serve");

                var command = Console.ReadLine();
                while (command.ToLower() != "exit")
                {
                    try
                    {
                        ProcessCommand(command);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: {0}", ex.Message);
                    }
                    command = Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fatal Error: {0}", ex.Message);
                Console.ReadKey();
            }
            finally {
                if (api != null) {
                    api.CloseClient();
                }
            }
        }

        private void ProcessCommand(string command)
        {
            if (command == "restart")
            {
                this.api.RestartEngine();
            }
                else if (command == "list")
            {
                Console.WriteLine("Not yet implemented.");
                //this.api.ProcessMessage(null);
            }
            else if (command == "do")
            {
                Console.WriteLine("Not yet implemented.");
                //this.api.
            }
            else {
                Console.WriteLine("The instruction '{0}' is invalid.", command);
            }
        }
    }
}
